package com.powernode.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.powernode.reggie.common.BaseContext;
import com.powernode.reggie.common.R;
import com.powernode.reggie.entity.AddressBook;
import com.powernode.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: www
 * @time: 2023/3/16 14:29
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 地址存储
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());//获取当前登录用户的id 设置为当前地址的uderid
        log.info("addressBook{}:",addressBook);
        addressBookService.save(addressBook);//数据库保存地址
        return R.success(addressBook);
    }

    /**
     * 地址展示
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> getList(){
        Long userId = BaseContext.getCurrentId();//获取当前的用户id
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId);//展示这个用户的地址
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook){//地址的id

        //现将该用户的所有地址默认值设为0，在将选中的地址默认值该为1
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());//找到所有该用户的地址
        wrapper.set(AddressBook::getIsDefault,0);//默认值设置为0

        //update address_book set is_default = 0 where user_id = ？
        addressBookService.update(wrapper);//更新

        //UPDATE address_book SET is_default=? WHERE id=?
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);

        return R.success(addressBook);
    }


    /**
     * 提交订单时获取用户默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());//根据用户地址查地址
        queryWrapper.eq(AddressBook::getIsDefault,1);//查默认地址

        AddressBook addressBook1 = addressBookService.getOne(queryWrapper);

        return R.success(addressBook1);
    }

    /**
     * 根据id查地址，用于回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> getAdd(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        if(addressBook != null){
            return R.success(addressBook);
        }
        return R.error("找不到");
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        if (addressBookService.updateById(addressBook)) {
            return R.success("保存成功");
        }
        return R.error("修改失败");
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping()
    public R<String> delete(@RequestParam Long ids){

        if (addressBookService.removeById(ids)) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

}
